﻿<%@ WebHandler Language="C#" Class="ContainerStepPlanLookBoard" %>

using System;
using System.Web;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using System.Web.SessionState;
using ReportBusiness;

public class ContainerStepPlanLookBoard : IHttpHandler
{

    private uMESReportOracleDAL dal = new uMESReportOracleDAL();
    private ContainerStepPlanLookBoardBusiness pb = new ContainerStepPlanLookBoardBusiness();
    public bool IsReusable { get { return false; } }
    public void ProcessRequest(HttpContext context)
    {
        //查询分厂名称
        if (context.Request.QueryString["type"] != null && context.Request.QueryString["type"] == "GetFactoryName")
        {
            var factoryid = context.Request.Form["factoryid"].ToString();
            DataTable dt = pb.GetFactoryInfo(factoryid);
            foreach (DataColumn col in dt.Columns)
            {
                col.ColumnName = col.ColumnName.ToLower();
            }
            var json = JsonConvert.SerializeObject(dt);
            context.Response.ContentType = "text/json";
            context.Response.Write(json.ToString());
            context.Response.End();
        }

        //查询分厂下的工区
        if (context.Request.QueryString["type"] != null && context.Request.QueryString["type"] == "GetWorkcentername")
        {
            var factoryid = context.Request.Form["factoryid"].ToString();
            DataTable dt = pb.GetWorkcentername(factoryid);
            foreach (DataColumn col in dt.Columns)
            {
                col.ColumnName = col.ColumnName.ToLower();
            }
            var json = JsonConvert.SerializeObject(dt);
            context.Response.ContentType = "text/json";
            context.Response.Write(json.ToString());
            context.Response.End();
        }

        if (context.Request.QueryString["type"] != null && context.Request.QueryString["type"] == "GetTotal")
        {
            Dictionary<string, object> para = new Dictionary<string, object>();
            //分厂
            if (context.Request.Form["factoryid"] != null)
            {
                para["factoryid"] = context.Request.Form["factoryid"].ToString();
            }
            //工区
            if (context.Request.Form["workcenterid"] != "全部")
            {
                para["workcenterid"] = context.Request.Form["workcenterid"];
            }
            //件号
            if (context.Request.Form["productname"] != "")
            {
                para["productname"] = context.Request.Form["productname"];
            }
            //是否重点件号
            if (context.Request.Form["isFocusProductname"] != "全部")
            {
                para["isFocusProductname"] = context.Request.Form["isFocusProductname"];
            }
            //流水卡号
            if (context.Request.Form["containername"] != "")
            {
                para["containername"] = context.Request.Form["containername"];
            }

            //批次是否合并
            para["isMerge"] = context.Request.Form["isMerge"];

            if (context.Request.Form["isInstore"] != "全部")
            {
                para["isInstore"] = context.Request.Form["isInstore"];
            }

            if (context.Request.Form["startDate"] != "")
            {
                para["startDate"] = context.Request.Form["startDate"];
            }

            if (context.Request.Form["endDate"] != "")
            {
                para["endDate"] = context.Request.Form["endDate"];
            }

            int total = pb.GetTotal(para);

            string json = "{\"total\":" + total + "}";

            context.Response.ContentType = "text/json";
            context.Response.Write(json);
            context.Response.End();
        }

        if (context.Request.QueryString["type"] != null && context.Request.QueryString["type"] == "GetData")
        {
            // pages=>总页数
            // containerCount=>每页查询出的批次条数（分页后的DataTable后的行数/7)
            // total=>总记录条数
            int pages = 0, containerCount = 0, total = 0, maxSequence = 0;

            DataTable dt;
            Dictionary<string, object> para = new Dictionary<string, object>();

            //当前第几页
            if (context.Request.Form["currentPage"] != null)
            {
                para["currentPage"] = context.Request.Form["currentPage"];
            }
            //每页批次条数
            if (context.Request.Form["pageSize"] != null)
            {
                para["pageSize"] = context.Request.Form["pageSize"];
            }
            //分厂
            if (context.Request.Form["factoryid"] != null)
            {
                para["factoryid"] = context.Request.Form["factoryid"].ToString();
            }
            //工区
            if (context.Request.Form["workcenterid"] != "全部")
            {
                para["workcenterid"] = context.Request.Form["workcenterid"];
            }
            //件号
            if (context.Request.Form["productname"] != "")
            {
                para["productname"] = context.Request.Form["productname"];
            }
            //是否重点件号
            if (context.Request.Form["isFocusProductname"] != "全部")
            {
                para["isFocusProductname"] = context.Request.Form["isFocusProductname"];
            }
            //流水卡号
            if (context.Request.Form["containername"] != "")
            {
                para["containername"] = context.Request.Form["containername"];
            }

            if (context.Request.Form["isInstore"] != "全部")
            {
                para["isInstore"] = context.Request.Form["isInstore"];
            }

            if (context.Request.Form["startDate"] != "")
            {
                para["startDate"] = context.Request.Form["startDate"];
            }

            if (context.Request.Form["endDate"] != "")
            {
                para["endDate"] = context.Request.Form["endDate"];
            }

            if (context.Request.Form["isMerge"] == "是")
            {
                //单件单卡根据当前序合并
                dt = pb.GetContainerStepPlanFinishInfoNew(para, out pages, out containerCount, out total, out maxSequence);
            }
            else
            {
                //单件单卡不合并
                dt = pb.GetJgjFocusContainerProcess(para, out pages, out containerCount, out total, out maxSequence);
            }

            foreach (DataColumn col in dt.Columns)
            {
                col.ColumnName = col.ColumnName.ToLower();
            }

            var json = JsonConvert.SerializeObject(dt);

            json = "{\"arr\":" + json.ToString() + ",\"pages\":" + pages + ",\"containerCount\":" + containerCount + ",\"total\":" + total + ",\"maxSequence\":" + maxSequence + "}";

            context.Response.ContentType = "text/json";
            context.Response.Write(json.ToString());
            context.Response.End();
        }
    }
}